//
//  THJCommitOrderView.m
//  THJXHW
//
//  Created by 河南德艺 on 2017/11/21.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJCommitOrderView.h"
#import "ZBNetworking.h"
#import "NSString+String.h"
@interface THJCommitOrderView()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * _tableview;
    NSString * addressUrl;
    NSString * addressId;
    NSString * trueName;
    NSString * telephone;
    NSString * sheng;
    NSString * shi;
}
@property(nonatomic,strong)NSArray * dataArr;
@property (weak, nonatomic) IBOutlet UILabel *shengshiLab;

@end

@implementation THJCommitOrderView
-(void)getAddressData{
    __weak typeof(self) weakself = self;
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString = addressUrl;
        request.apiType = ZBRequestTypeCache;
    }  success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        weakself.dataArr = receiveData[@"data"];
        [_tableview reloadData];
        NSLog(@"datacount-%ld",self.dataArr.count);
        if(weakself.dataArr.count == 0){
            NSString * url = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=Area&r=Area"];
            addressUrl = url;
            self.shengshiLab.text = [NSString stringWithFormat:@"%@%@",sheng,shi];
            [_tableview removeFromSuperview];
        }
        
    } failed:^(NSError *error){
    }];
    
    
}
-(void)getPersonInfo{
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=member&r=member&m=info"];
        request.apiType = ZBRequestTypeRefresh;
        [request setValue:[THJTools getToken] forHeaderField:@"token"];
    }  success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        if([receiveData[@"status"] isEqual:@"200"]){
            trueName = receiveData[@"data"][@"truename"];
            NSLog(@"gerenxinxi-%@",receiveData);
        }else{
            ULog(@"请重新登录");
        }
       
    } failed:^(NSError *error){
    }];
}
-(void)setAddressView{
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self addSubview:_tableview];
    
   
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = self.dataArr[indexPath.row][@"areaname"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
  
    NSDictionary * dic = self.dataArr[indexPath.row];
    addressId =  dic[@"areaid"];
    addressUrl =  [NSString stringWithFormat:@"%@index.php?f=Area&r=Area&parentid=%@",THJUrlDomain,dic[@"areaid"]];
    [self getAddressData];
    if(sheng){
        shi = dic[@"areaname"];
    }else{
        sheng = dic[@"areaname"];
    }
    NSLog(@"shengshi");
}

- (IBAction)reSetAddressAction:(id)sender {
    sheng = @"";
    shi = @"";
    [self getAddressData];
    [self setAddressView];
}

- (IBAction)cancelBtnAction:(id)sender {
    [self removeFromSuperview];
}
+(THJCommitOrderView*)instancView{
    
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"THJCommitOrderView" owner:nil options:nil];
    return [nibView objectAtIndex:0];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        
        [self didload];
        [self getAddressData];
        [self getPersonInfo];
    }
    return self;
}
-(void)didload{
    self.TF1.keyboardType = UIKeyboardTypeNumberPad;
    self.TF2.keyboardType = UIKeyboardTypeNumberPad;
    self.TF4.keyboardType = UIKeyboardTypeNumberPad;
    NSLog(@"self.itemid=%@",self.itemid);
    addressUrl = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=Area&r=Area"];
    [self.titleImgV sd_setImageWithURL:[NSURL URLWithString:self.infoDic[@"logo"]] placeholderImage:kGetImage(@"占位图")];
    self.titleLab.text = self.infoDic[@"company"];
}
- (IBAction)commitOrderAction:(id)sender {
    NSLog(@"JKL-%@",self.infoDic);
    BOOL isPhoneNum = [NSString isMobileNumber:self.TF4.text];
    if(!trueName){
        ULog(@"请重新登录");
        
        return;
    }
    if (!self.TF1.text.length) {
        ULog(@"请填写订货量");
        return;
    }else if(!self.TF2.text.length){
        ULog(@"请填写理想价格");
        return;
    }else if(!self.TF3.text.length){
        ULog(@"请填写详细地址");
        return;
    }else if(!self.TF4.text.length ){
        ULog(@"请填写联系方式");
        return;
    }else if(!isPhoneNum){
        ULog(@"联系方式格式不对");
        return;
    }else if([self.shengshiLab.text isEqual:@"省     市"]){
        ULog(@"请填写省市");
        return;
    }
    NSString * guige =  [THJTools guigeTypeChangeToStrWithType:self.infoDic[@"gg"]];
    NSLog(@"产品详细-%@",self.infoDic);
    NSDictionary * paraDic=@{
                              @"title":[NSString stringWithFormat:@"求购%@,%@",self.infoDic[@"catname"],guige],
                              @"catid":self.infoDic[@"catid"],
                              @"areaid":addressId,
                              @"price":self.TF2.text,
                              @"truename":trueName,
                              @"telephone":self.TF4.text,
                              @"mobile":self.TF4.text,
                              @"address":[NSString stringWithFormat:@"%@%@%@",sheng,shi,self.TF3.text],
                              @"cainum":self.TF1.text,
                              @"content":[NSString stringWithFormat:@"求购%@%@吨，地址：%@%@%@",self.catname,self.TF1.text,sheng,shi,self.TF3.text],
                              @"sell_id":self.itemid,
                              @"catname":self.catname
                              };
    NSLog(@"tijiao-%@",paraDic);
    NSString * urlstr = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=My_buy_6&r=My_buy_6&m=sadd"];
    NSString * token = [[NSUserDefaults standardUserDefaults]objectForKey:@"token"];
    

    [THJNetWork postDataFromUrl:urlstr parameters:paraDic Token:token callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"返回提交-%@-%@",receiveData,error);
        if([receiveData[@"status"] isEqual:@"200"]){
            ULog(@"求购成功");
            [self removeFromSuperview];
        }
    }];
}
@end
